import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

// 导入路由页面
import login from "../components/login/login"
// import regist from '../components/login/regist'
import regist from '../components/login/registry'
import index from '../components/main/index'
import chat from '../components/main/child/chat'
import friend from '../components/main/child/friend'

const router = new Router({
    routes: [
        {
            path: "/login",
            component: login
        },
        {
            path: '/regist',
            component: regist
        },
        {
            path: '/index',
            component: index,
            redirect: '/chat',
            children: [
                {
                    path: '/chat',
                    component: chat
                },
                {
                    path: '/friend',
                    component: friend
                }
            ]
        }
    ],
    linkActiveClass: 'active'
})

router.beforeEach((to, from, next) => {
    let user = sessionStorage.getItem('loginUser');
    if (to.path == '/login' || to.path == '/regist') {
        next();
        return;
    }
    if (!user) {
        next('/login');
        return;
    }
    next();
})
//默认打开的页面
// router.push({ path: "/index" })

export default router